package com.baoli.blzj.adapter.home;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.baoli.blzj.bean.home.HomeMenuBean;
import com.baoli.blzj.customviews.ProgressDialogFragment;
import com.bumptech.glide.Glide;
import com.baoli.blzj.Constants;
import com.baoli.blzj.R;

import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * 主页面菜单适配器 by John
 */
public class HomeMenuGridViewAdapter extends BaseAdapter implements Constants {

    private Context context;

    private List<HomeMenuBean> list;

    private LayoutInflater inflater;

    private ProgressDialogFragment pf;

    public SharedPreferences spn;

    public SharedPreferences.Editor editor;

    public HomeMenuGridViewAdapter(Context context, List<HomeMenuBean> list) {
        spn = context.getSharedPreferences(SPN_BLZJ, MODE_PRIVATE);
        editor = spn.edit();
        pf = new ProgressDialogFragment();
        this.context = context;
        this.list = list;//new ArrayList<>();
        this.inflater = (LayoutInflater) LayoutInflater.from(context).getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setList(List<HomeMenuBean> hBean) {
        list.clear();
        append(hBean);
    }

    public void append(List<HomeMenuBean> hBean) {
        list.addAll(hBean);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        convertView = inflater.inflate(R.layout.activity_home_menu_item, parent, false);
        ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
        //ImageView open = (ImageView) convertView.findViewById(R.id.open);
        TextView name = (TextView) convertView.findViewById(R.id.name);

        Glide.with(context)
                .load(list.get(position).getFuc_img())
                .crossFade()
                .animate(R.anim.anim_photo)
                .placeholder(R.drawable.ic_launcher)
                .error(R.drawable.ic_launcher)
                .into(icon);

        name.setText(list.get(position).getFun_name());

        /*if ("1".equals(list.get(position).getFuc_type())) {
            open.setVisibility(View.GONE);
        } else {
            open.setVisibility(View.VISIBLE);
        }*/


        return convertView;
    }
}
